﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using PH.Model.Analyses;
using PH.Model.Analyses.Schools;
using Utils;
namespace PH.Data.Analyses
{
    public class PSCommand
    {
        /// <summary>
        /// 按班级分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4ClassCollection GetPS4Class(AnalysisQuery sea)
        {
            var ret = new S4ClassCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Class> q = db.DataContext.PS4Class;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.ClassName == sea.Key);
                    q = q.OrderBy(p => p.GradeCode).ThenBy(p => p.CIndex);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4ClassModel, PS4Class, S4ClassCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 按性别分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4SexCollection GetPS4Sex(AnalysisQuery sea)
        {
            var ret = new S4SexCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Sex> q = db.DataContext.PS4Sex;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    q = q.OrderBy(p => p.Sex);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4SexModel, PS4Sex, S4SexCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 按年级分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4GradeCollection GetPS4Grade(AnalysisQuery sea)
        {
            var ret = new S4GradeCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Grade> q = db.DataContext.PS4Grade;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    q = q.OrderBy(p => p.GradeCode);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4GradeModel, PS4Grade, S4GradeCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 按老师分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4TeacherCollection GetPS4Teacher(AnalysisQuery sea)
        {
            var ret = new S4TeacherCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Teacher> q = db.DataContext.PS4Teacher;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    if (sea.SchoolGuid != Guid.Empty)
                        q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.TeacherName == sea.Key);
                    q = q.OrderBy(p => p.SchoolID).ThenBy(p => p.TeacherName);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4TeacherModel, PS4Teacher, S4TeacherCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 分析样品采集总进度
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4SampleGroupCollection GetPS4SampleGroup(S4SampleQuery sea)
        {
            var ret = new S4SampleGroupCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4SampleGroup> q = db.DataContext.PS4SampleGroup;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.ClassName == sea.Key);
                    if (sea.Finish != null)
                    {
                        if (sea.Finish.Value)
                            q = q.Where(p => p.Balance == 0);
                        else
                            q = q.Where(p => p.Balance != 0);
                    }
                    q = q.OrderBy(p => p.GradeCode).ThenBy(p => p.CIndex);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4SampleGroupModel, PS4SampleGroup, S4SampleGroupCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 分析样品采集进度明细
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4SampleCollection GetPS4Sample(S4SampleQuery sea)
        {
            var ret = new S4SampleCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Sample> q = db.DataContext.PS4Sample;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    if (sea.ClassID > 0)
                        q = q.Where(p => p.ClassID == sea.ClassID);
                    q = q.OrderBy(p => p.SubjectID);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4SampleModel, PS4Sample, S4SampleCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 学生个人数据
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4StudentCollection GetPS4Student(S4StudentQuery sea)
        {
            var ret = new S4StudentCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Student> q = db.DataContext.PS4Student;
                    if (sea.ArchiveGuid != null && sea.ArchiveGuid != Guid.Empty)
                        q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    if (sea.SchoolGuid != Guid.Empty)
                        q = q.Where(p => p.SchGuid == sea.SchoolGuid);
                    if (!string.IsNullOrEmpty(sea.Key))
                        q = q.Where(p => p.StudentNo == sea.Key);
                    if (sea.ClassID > 0)
                        q = q.Where(p => p.ClassID == sea.ClassID);
                    if (!string.IsNullOrEmpty(sea.Key))
                    {
                        if (sea.Key.CheckIDNo())
                            q = q.Where(p => p.StudentNo == sea.Key);
                        else
                            q = q.Where(p => p.RealName == sea.Key);
                    }
                    q = q.OrderByDescending(p => p.CreatedOn);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4StudentModel, PS4Student, S4StudentCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
        /// <summary>
        /// 通过身份证取得学生成绩
        /// </summary>
        /// <param name="studentno"></param>
        /// <returns></returns>
        public static S4StudentCollection GetPS4Student(string studentno)
        {
            var ret = new S4StudentCollection();
            if (studentno.CheckIDNo())
            {
                using (DBCentre db = new DBCentre())
                {
                    IQueryable<PS4Student> q = db.DataContext.PS4Student;
                    q = q.Where(p => p.StudentNo == studentno);
                    q = q.OrderByDescending(p => p.CreatedOn);
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4StudentModel, PS4Student, S4StudentCollection>(lst);
                    ret.TotalRecordsCount = lst.Count;
                }
            }
            else
                throw new PhException("身份证号无效");
            return ret;
        }
        /// <summary>
        /// 按项目分析
        /// </summary>
        /// <param name="sea"></param>
        /// <returns></returns>
        public static S4SubjectCollection GetPS4Subject(AnalysisQuery sea)
        {
            var ret = new S4SubjectCollection();
            if (sea != null)
            {
                using (DBCentre db = new DBCentre())
                {
                    var n = 0;
                    IQueryable<PS4Subject> q = db.DataContext.PS4Subject;
                    q = q.Where(p => p.ArchiveGuid == sea.ArchiveGuid);
                    if (sea.SchoolGuid != Guid.Empty)
                        q = q.Where(p => p.SchoolGuid == sea.SchoolGuid);
                    q = q.OrderBy(p => p.SubjectID);
                    if (sea.PageSize > 0)
                    {
                        n = q.Count();
                        q = q.Skip(sea.PageSize * sea.PageIndex).Take(sea.PageSize);
                    }
                    var lst = q.ToList();
                    ret = Convertor.ConvertCollection<S4SubjectModel, PS4Subject, S4SubjectCollection>(lst);
                    ret.TotalRecordsCount = n == 0 ? lst.Count : n;
                }
            }
            return ret;
        }
    }
}
